/**
 * @param {number[][]} grid
 * @param {number} k
 * @return {number[][]}
 */
export var shiftGrid = function (grid, k) {
  const [m, n] = [grid.length, grid[0].length]
  k = k % (m * n)
  const ans = new Array(m).fill(0).map(() => new Array(n).fill(0))
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      const idx = (i * n + j - k + m * n) % (m * n)
      ans[i][j] = grid[(idx - (idx % n)) / n][idx % n]
    }
  }
  return ans
}
